package com.tianyi.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.tianyi.dao.entity.orderProducts.Orderproducts;

import java.awt.font.FontRenderContext;
import java.util.List;

public class PdfUtil {

        // 定义全局的字体静态变量
        private static Font keyfont;
        private static Font smallfont;
        private static int maxWidth = 550;

        static {
            try {
                // 设置字体
                BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                keyfont = new Font(bfChinese, 6, Font.BOLD);
                smallfont = new Font(bfChinese, 5, Font.BOLD);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 生成PDF文件
        public static void generatePDF(Document document, List<Orderproducts> productResults, PdfInfos pdfInfos) throws Exception {

            // 表格上方的起始段落
            Paragraph paragraph = new Paragraph("预约借衣管理-借衣单", keyfont);
            paragraph.setAlignment(1); //设置文字居中 0靠左   1，居中     2，靠右

            // 直线
            Paragraph p1 = new Paragraph();
            p1.add(new Chunk(new LineSeparator()));

            // 点线
            Paragraph p2 = new Paragraph();
            p2.add(new Chunk(new DottedLineSeparator()));

            // 超链接
            //Anchor anchor = new Anchor("baidu");
            //anchor.setReference("www.baidu.com");

            PdfPTable table = createTable(new float[] { 100, 100, 120, 100, 120, 100, 120, 100, 120 });

            table.addCell(createCell("表单性质", keyfont, Element.ALIGN_CENTER));
            PdfPCell pCell1 = createCell("此表为借衣单，打印一式两份，一份由经手人备份，另一份随包裹发出", keyfont, Element.ALIGN_CENTER);
            pCell1.setColspan(8);
            table.addCell(pCell1);

            table.addCell(createCell("基础信息", keyfont, Element.ALIGN_CENTER));
            PdfPCell pCell2 = createCell(" ", keyfont, Element.ALIGN_CENTER);
            pCell2.setColspan(6);
            table.addCell(pCell2);

            table.addCell(createCell("经手人", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getOperator(), keyfont, Element.ALIGN_CENTER));

            PdfPCell pCell3 = createCell("借拍信息", keyfont, Element.ALIGN_CENTER);
            pCell3.setRowspan(3);
            table.addCell(pCell3);

            table.addCell(createCell("借用人员", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getLendPerson(), keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("微信ID", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" ", keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("所属公司", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getCompanyname(), keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("联系电话", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getReceivePhonenumber(), keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("试衣日期", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getTryclothestime(), smallfont, Element.ALIGN_CENTER));

            table.addCell(createCell("归还日期", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getReturnTime(), smallfont, Element.ALIGN_CENTER));

            table.addCell(createCell("拍摄日期", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getPhotoShootTime(), smallfont, Element.ALIGN_CENTER));

            table.addCell(createCell("出版日期", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getPublishTime(), smallfont, Element.ALIGN_CENTER));

            table.addCell(createCell("借用艺人", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getStar(), keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("活动/拍摄", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" ", keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("拍摄主题", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" "+pdfInfos.getPhotoTheme(), keyfont, Element.ALIGN_CENTER));

            table.addCell(createCell("备注", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(" ", keyfont, Element.ALIGN_CENTER));

            PdfPCell pCell4 = createCell("产品信息", keyfont, Element.ALIGN_CENTER);
            pCell4.setRowspan(productResults.size()+1);
            table.addCell(pCell4);

            table.addCell(createCell("图片", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("品牌", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("名称", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("货号", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("价格", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("颜色", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("尺码", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("数量", keyfont, Element.ALIGN_CENTER));

            for(Orderproducts product : productResults){
                table.addCell(createCell(" ", keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getBrandid(), keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getProductname(), keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getProductnumber(), keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getPrice(), keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getColor(), keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getSize(), keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(" "+product.getUsedcount(), keyfont, Element.ALIGN_CENTER));
            }

            PdfPCell pCell5 = createCell("快递信息", keyfont, Element.ALIGN_CENTER);
            pCell5.setRowspan(2);
            table.addCell(pCell5);

            PdfPCell pCell6 = createCell("送达日期", keyfont, Element.ALIGN_CENTER);
            pCell6.setRowspan(1);
            table.addCell(pCell6);

            PdfPCell pCell7 = createCell(" "+pdfInfos.getArriveTime(), smallfont, Element.ALIGN_CENTER);
            pCell7.setRowspan(1);
            table.addCell(pCell7);

            PdfPCell pCell8 = createCell("配送方式", keyfont, Element.ALIGN_CENTER);
            pCell8.setRowspan(1);
            table.addCell(pCell8);

            PdfPCell pCell9 = createCell(" "+pdfInfos.getSendMethod(), keyfont, Element.ALIGN_CENTER);
            pCell9.setRowspan(1);
            table.addCell(pCell9);

            PdfPCell pCell10 = createCell("配送地址", keyfont, Element.ALIGN_CENTER);
            pCell10.setRowspan(1);
            table.addCell(pCell10);

            PdfPCell pCell11 = createCell(" "+pdfInfos.getReceiveAddress(), keyfont, Element.ALIGN_CENTER);
            pCell11.setRowspan(1);
            pCell11.setColspan(4);
            table.addCell(pCell11);

            table.addCell(createCell("快递单号", keyfont, Element.ALIGN_CENTER));
            PdfPCell pCell12 = createCell(" ", keyfont, Element.ALIGN_CENTER);
            pCell12.setRowspan(1);
            pCell12.setColspan(3);
            table.addCell(pCell12);

            table.addCell(createCell("快递费用", keyfont, Element.ALIGN_CENTER));
            PdfPCell pCell13 = createCell(" ", keyfont, Element.ALIGN_CENTER);
            pCell13.setRowspan(1);
            pCell13.setColspan(3);
            table.addCell(pCell13);

            PdfPCell pCell14 = createCell("READY TO LEND 竭诚为您服务", keyfont, Element.ALIGN_CENTER);
            pCell14.setRowspan(1);
            pCell14.setColspan(9);
            table.addCell(pCell14);

            document.add(paragraph);
            document.add(p2);
            document.add(p1);
            document.add(table);
        }


        // 创建基本单元格
        private static PdfPCell createCell(String value, Font font, int align) {
            PdfPCell cell = new PdfPCell();
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(align);
            cell.setPhrase(new Phrase(value, font));
            return cell;
        }
        // 初始化表格
        private static PdfPTable createTable(float[] widths) {
            PdfPTable table = new PdfPTable(widths);
            try {
                table.setTotalWidth(maxWidth);
                table.setLockedWidth(true);
                table.setHorizontalAlignment(Element.ALIGN_CENTER);
                table.getDefaultCell().setBorder(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return table;
        }
}